package com.chanyachting.emall.action.index;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Channel;
import com.chanyachting.emall.domain.Newsactivity;
import com.chanyachting.emall.form.NewsActivityForm;
import com.chanyachting.emall.service.ChannelService;
import com.chanyachting.emall.service.NewsActivityService;
import com.chanyachting.emall.serviceImpl.ChannelServiceImpl;
import com.chanyachting.emall.serviceImpl.NewsActivityServiceImpl;

public class QueryActivityNewsAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//首页表示记录数
		int COMMON_PAGE_SIZE = 8;
		
		NewsActivityForm naForm = (NewsActivityForm) form;
		NewsActivityService naService=new NewsActivityServiceImpl();
		ChannelService cService = new ChannelServiceImpl();
		
		int channelId = naForm.getChannelId();
		
		Integer subChannelId = naForm.getSubChannelId();
		if (subChannelId == null) {
			return mapping.findForward("error");
		}
		Channel c = cService.getOneChannel(new Channel(channelId));
		if (c == null) {
			return mapping.findForward("error");
		}
		List<Channel> subChannelList = cService.getSubChannel(c);
		
		if (subChannelList == null || subChannelList.size() == 0) {
			return mapping.findForward("error");
		}
		//判断指定的sub频道是否存在
		boolean isExistSubChannel = false;
		for(Channel cn:subChannelList) {
			if (cn == null || cn.getId() == null) {
				continue;
			}
			if (cn.getId().equals(subChannelId)) {
				naForm.setSubChannelName(cn.getName());
				isExistSubChannel = true;
				break;
			}
		}
		if (isExistSubChannel == false) {
			return mapping.findForward("error");
		}
		
		naForm.setCurrentPage(1);
		int currentPage = naForm.getCurrentPage();
		
		int pageSize = COMMON_PAGE_SIZE;
		
		int recordCnt = naService.getNewsCountBySubChannalId(String.valueOf(subChannelId),true);
		
		int pageCount = recordCnt%pageSize == 0?recordCnt/pageSize:(recordCnt/pageSize + 1);
		
		if (currentPage < 1) {
			currentPage = 1;
		} else if (currentPage > pageCount) {
			currentPage = 1;
		}
		
		Map<String,Newsactivity> channelList = new TreeMap<String,Newsactivity>();
		List<Newsactivity> tmpNewsact = naService.getNewsBySubChannalId(String.valueOf(subChannelId), currentPage, pageSize,true);
		
		boolean peopleNews = false;
		for(int i=0;i<tmpNewsact.size();i++) {
			if (tmpNewsact.get(i).getImgUrl() != null && !"".equals(tmpNewsact.get(i).getImgUrl())) {
				if (i > 2) {
					break;
				}
				peopleNews = true;
			}
			String newsTitle = tmpNewsact.get(i).getTitle();
			if (newsTitle != null && newsTitle.length() > 14) {
				newsTitle = newsTitle.substring(0, 14) + "";
			}
			tmpNewsact.get(i).setTitle(newsTitle);
			channelList.put(naForm.getSubChannelName() + "&" + i, tmpNewsact.get(i));
		}
		
		naForm.setSubChannelList(subChannelList);
		naForm.setChannelList(channelList);
		naForm.setChannelName(c.getName());
		
		if (peopleNews) {
			return mapping.findForward("doPeopleNewsAtIndex");
		}
		
		System.out.println("naForm.getNewsType()=" + naForm.getNewsType());
		return mapping.findForward("doActivityAtIndex");
	}

}
